from django.contrib import admin
from goods.models import SKU
from django.core.cache import caches
# Register your models here.

INDEX_CACHE = caches["goods_index"]
DETAIL_CACHE = caches["goods_detail"]

@admin.register(SKU)
class SKUAdmin(admin.ModelAdmin):
    #重写save_model方法，更新mysql，清除redis
    def save_model(self, request, obj, form, change):
            #更新mysql
            super().save_model(request,obj,form,change)
            #清除首页redis缓存
            INDEX_CACHE.clear()

            #清除详情页的缓存 del key
            key = "gd%s"%obj.id
            DETAIL_CACHE.delete(key)
            print("更新数据时，详情页缓存清除--")

    def delete_model(self, request, obj):
        super().delete_model(request,obj)
        INDEX_CACHE.clear()
